__easy.scriptpath = __easy.exepath
__easy.commonpath = __easy.exepath
local function shortpath(path, toAsc)
	if #__easy.exepath == 0 then return path end
	if toAsc then
		return string.gsub(tostring(path), Variant_Utf82Asc(__easy.exepath), '') 
	else
		return string.gsub(tostring(path), __easy.exepath, '')
	end
end

local function tracebackex()
	local level = 3
	local ret = "stack traceback:\n"
	local firsterr = true
	while true do
		--get stack info
		local info = debug.getinfo(level, "Sln")
		if not info or not info.name then break end
		if info.what == "C" then -- C function
			ret = ret .. string.format("[C]: in function '%s'\n", info.name)
		else -- Lua function
			ret = ret .. string.format("%s:%d: in function '%s'\n", shortpath(info.short_src, true), info.currentline, info.name)
		end
		--get local vars
		local i = 1
		while true do
			local name, value = debug.getlocal(level, i)
			if not name then break end
			if name == 'err' and firsterr then
				firsterr = true
				break
			end
			if name ~= 'self' and name ~= 'modname' and name ~= 'file' then -- and name ~= '(temporary)' 
				if name == 'filename' then
					value = shortpath(value)
				else
					value = tostring(value)
					if name == 'str' then
						value = string.sub(value, 1, 128)
					elseif #value > 64 then
						value = value:sub(1, 64)
					end
					if string.find(value, '\n') then
						value = string.gsub(value, '\n', '\n\t')
					end
					if #value >= 64 then
						value = value .. "\n\t..."
						value = value .. "..."
					end
				end
				ret = ret .. string.format("        %s =  %s\n", name, value)
			end
			i = i + 1
		end
		level = level + 1
	end
	return ret
end

local errored = false
__easy.traceback = function(msg)
	if errored then 
		return
	end
	msg = shortpath(msg, true)
	local title = msg
	msg = msg .. '\n' .. tracebackex()
	cc_msgbox(Variant_Utf82Asc(title), Variant_Utf82Asc(msg))
	errored = true
end

local function doScript(filename, modname)
	local file = io.open(Variant_Utf82Asc(filename), "rb")
	local str = file:read("*a")
	file:close()
	local func, err = load(str, modname)
	if func then
	    local ret = { xpcall(func, __easy.traceback) }
		if ret[1] and ret[2] then
			return table.unpack(ret, 2)
	    end
	    return modname
	else
		__easy.traceback(err)
	end
end

local function fileexist(filename)
	local file = io.open(Variant_Utf82Asc(filename), "rb")
	if not file then  return false end
	file:close()
	return true
end

local function searchScript(modname)
    local matchPaths = {
		[1] = __easy.scriptpath,
		[2] = __easy.commonpath
	}
	local path
	local realname
	for k, v in ipairs(matchPaths) do
		realname = v .. modname
		if fileexist(realname .. '.lua') then
			path = realname
			break
		end
	end
	return path
end

setmetatable(package.loaded, { __index = function(t, k)
	if not package.preload[k] then
		local v = searchScript(k)
		if v then
			v = doScript(v .. '.lua', k)
			if v then
				rawset(t, k, v)
				return v
			end
		end
	end
end 
})

print(__easy.luaversion)
print(__easy.ggeversion)

if __easy.mainlua == '' then
	if not gge_createEngine(640, 480, 60) then os.exit() end
else
	doScript(__easy.mainlua, 'main')
end

function gge_frame()
	if not mainLoop then return false end
	xpcall(mainLoop, __easy.traceback, Timer_GetDelta(), Input_GetMousePosX(), Input_GetMousePosY())
	return false
end


function gge_message(HWND, UMSG, WPARAM, LPARAM)
	return false
end


function gge_exit()
	if not onExit then return true end
	local ret = { xpcall(onExit, __easy.traceback) }
	if ret[1] then
        return table.unpack(ret, 2)
	end
	return ret[1]
end

gge_callback()
